package com.nbjtjc.safe.service.sys;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.sys.SysPostMapper;
import com.nbjtjc.safe.model.sys.SysPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 岗位
 *
 * @auther guqianbin
 * @date 2018/8/8 14:17
 **/
@Service
public class SysPostService {

    @Autowired
    private SysPostMapper sysPostMapper;


    public Result add(SysPost sysPost) {
        Result result = new Result();
        List<SysPost> sysPosts = sysPostMapper.findByName(sysPost.getPostName());
        if (CollectionUtil.isNotEmpty(sysPosts)) {
            result.setFalse("已经有相同的岗位名");
            return result;
        }
        sysPostMapper.insertSelective(sysPost);
        result.setData(sysPost);
        return result;
    }

    public Result update(SysPost sysPost) {
        Result result = new Result();
        List<SysPost> sysPosts = sysPostMapper.findByName(sysPost.getPostName());
        if (CollectionUtil.isNotEmpty(sysPosts)) {
            if(!sysPosts.get(0).getId().equals(sysPost.getId())){
                result.setFalse("已经有相同的岗位名");
                return result;
            }
        }
        sysPostMapper.updateByPrimaryKeySelective(sysPost);
        result.setData(sysPost);
        return result;
    }

    public Result delete(SysPost sysPost) {
        Result result = new Result();
        sysPostMapper.deleteByPrimaryKey(sysPost.getId());
        return result;
    }

    public PageInfo find(SysPost sysPost){
        PageHelper.startPage(sysPost.getPageCurrent(),sysPost.getPageSize());
        List<SysPost> sysPosts=sysPostMapper.search(sysPost);
        return new PageInfo<>(sysPosts);
    }


    @Transactional
    public boolean copySysPost(int fromCompanyId,int toCompanyId){
        SysPost fromPost=new SysPost();
        fromPost.setCompanyId(fromCompanyId);
        List<SysPost> fromPosts=sysPostMapper.select(fromPost);
        if(CollectionUtil.isEmpty(fromPosts)){
            return true;
        }
        List<SysPost> toPosts=new ArrayList<>();
        fromPosts.forEach(post->{
            SysPost toPost=new SysPost();
            toPost.setCompanyId(toCompanyId);
            toPost.setInspectCount(post.getInspectCount());
            toPost.setInspectCountTimeLimit(post.getInspectCountTimeLimit());
            toPost.setPostName(post.getPostName());
            toPost.setSelfInspectCount(post.getSelfInspectCount());
            toPost.setSelfInspectTimeLimit(post.getSelfInspectTimeLimit());
            toPost.setSuperviseInspectCount(post.getSuperviseInspectCount());
            toPost.setSuperviseInspectTimeLimit(post.getSuperviseInspectTimeLimit());
            toPosts.add(toPost);
        });
        sysPostMapper.insertList(toPosts);
        return true;
    }

}
